package singleton.type5;

/**
 * @author 龙超
 * @date 2020/7/1 15:27
 * @Description 使用线程不安全的懒汉模式
 */
public class Singleton5 {
    public static void main(String[] args) {
        System.out.println("使用线程不安全的懒汉式");
        Singleton instance = Singleton.getInstance();
        Singleton instance1 = Singleton.getInstance();
        System.out.println(instance == instance1);//不一定恒为true了
        System.out.println(instance.hashCode());
        System.out.println(instance1.hashCode());
    }
}

class Singleton {
    private static Singleton instance;

    private Singleton() {
    }

    //使用同步代码块
    public static Singleton getInstance() {
        if (instance == null) {
            synchronized (Singleton.class) {
                instance = new Singleton();
            }
        }
        return instance;
    }
}
